package a02;

import java.util.List;

public class AlphaBetaOthelloControl extends OthelloControl {

    private static final long serialVersionUID = 1L;
    private AlphaBetaOthello kiOthello = new AlphaBetaOthello();

    public AlphaBetaOthelloControl() {
	this(new Othello());
    }

    public AlphaBetaOthelloControl(Othello game) {
	super(game, 9);
	setGameMode1Player(true);
	kiOthello = new AlphaBetaOthello();
    }

    @Override
    public void whenMousePressed(byte c, byte r) {	
	if (this.getBoard().getGame().ended()) {
	    return;
	}
	if (this.getBoard().getGame().currentPlayer() == AbstractRegularGame
		.getTwo()) {	  
	    Othello tmp = (Othello) kiOthello.doBestMove(this.getBoard().getGame());	    
	    this.getBoard().setGame(tmp);
	    this.refresh();

	} else {
	    final List<OthelloMove> ms = this.getBoard().getGame().moves();
	    if (ms.get(0) instanceof NoMove) {
		this.getBoard()
			.setGame(
				((Othello) this.getBoard().getGame()).doMove(ms
					.get(0)));
	    }
	    if (getStack().isEmpty())
		kiOthello.setDepth(getMenu().getSkillLevel());
	    super.whenMousePressed(c, r);	   
	}
    }

    public int getDepth() {
	return kiOthello.getDepth();
    }

    public void setDepth(int depth) {
	kiOthello.setDepth(depth);
    }

    @Override
    public void refresh() {
	String status = "";
	boolean thinking = false;
	if (this.getBoard().getGame().currentPlayer() == AbstractRegularGame
		.getOne()) {
	    status = "Sie (Spieler "
		    + this.getBoard().getPlayerColorName(
			    this.getBoard().getGame().currentPlayer())
		    + ") sind an der Reihe";
	} else {
	    status = "Computer ist dran - bitte klicken!";
	    thinking=true;
	}

	getMenu().setStatus(status,thinking);
	System.out.println(".");
    }
}
